package test.base.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;

import java.util.List;

/**
 * <br/>
 * Created by ZXFeng on 2022/7/19.
 */
public class Byte2IntegerCodec extends ByteToMessageCodec<Integer> {

    @Override
    public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        if (in.readableBytes() >= 4) {
            int i = in.readInt();
            System.out.println("decode i = " + i);
            out.add(i);
        }
    }

    @Override
    public void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out) {
        out.writeInt(msg);
        System.out.println("encode i = " + msg);
    }

}